<template>
	<view>
		
		<!-- nav栏 -->
		<scroll-view scroll-x="true" show-scrollbar='false' class="nav-box">
			
			<view @click="navClick(itme.id)" v-for="(itme,index) in navArr" :key="itme.id" 
			:class="itme.id === Navindex ? 'active' :''"
			class="nav-itme">{{itme.league}}
			</view>

		</scroll-view>
	
		<!-- CBA盒子 -->
		<view class="cba-box">
			
			<view class="cba-content">
				<view @click="itmeClick(itme.id)" class="scroll-itme" v-for="itme in dataArr" :key="itme.id">
					
					<view class="nba-logo">
					<image  class="nba-img" :src="itme.homePicUrl" mode="aspectFill"></image>
						<text class="number">{{itme.homeScore || 0 }} : {{itme.guestScore || 0 }}</text>
					<image class="nba-img" :src="itme.guestPicUrl" mode="aspectFill"></image>						
					</view>
					
					<view class="nba-team">
						<text class="nba-team-name-left">{{itme.homeName}}</text>
						<text class="nba-team-name-right">{{itme.guestName}}</text>
					</view>
					<view v-if="itme.statusDesc === '已完赛' || itme.statusDesc ==='已结束'" class="roundType">{{itme.statusDesc}}</view> 
					<view v-else class="roundType active">未开始</view>
					<!-- <view class="roundType">{{itme.roundType}}</view> -->
					<view class="startTime">{{itme.startTime}}</view>
					
				</view>
				
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//导航栏数据
				navArr:[],
				Navindex:0,
				//要渲染的赛程数组
				dataArr:[],
				//总共的数据
				allArr:[]
			};
		},
		onLoad() {
			this.Navindex = 3400
		//加载页面获取本地存放的navIndex
		  try {
		  	const value = uni.getStorageSync('navIndexKey');
		  	if (value) {
		  		this.Navindex = value
		  		console.log(value);
		  	}
		  } catch (e) {
		  	// error
		  }
		 
		 this.getNavData()
		 
		},
		onShow() {
			//页面再次进入时获取navIndex
			try {
				const value = uni.getStorageSync('navIndexKey');
				if (value) {
					this.Navindex = value
					console.log(value);
				}
			} catch (e) {
				// error
			}
		},
		onHide() {
			//页面离开时设置navIndex
			try{
				uni.setStorageSync('navIndexKey',this.Navindex)
			} catch(error){
				console.log(error);
			}
		},
		methods:{
			//获取导航数据和赛事数据
			getNavData(){
				let time =  Date.now()
				console.log(time);
				uni.request({
					url: `https://cbs-i.sports.cctv.com/cache/0fe461738f548ffb6227a83776895fad?ran=${time}`,
					//https://cbs-i.sports.cctv.com/cache/ef972bd6675867a16c8fd5c0b4597a56?ran=1668853113695
					method: 'GET',
					data: {},
					success: res => { 
						console.log(res);				
						//过滤数组
						 let arr = res.data.results.filter((value)=>{
							return value.id === 0 || value.id === 3238 || value.id === 3325 || value.id === 3246 || value.id === 3239 || value.id === 3400 || value.id === 4314 || value.id === 4315
						});
						//将过滤好的数组按照自己想要的顺序排列
						arr.sort((a,b)=>{
							let sortArr = ['热门','世界杯','NBA','CBA','英超','中超','欧冠','意甲']
							return sortArr.indexOf(a.league) - sortArr.indexOf(b.league)
						})
						//将排序好的数组赋值给导航数组
						this.navArr = arr
						console.log(this.navArr,'this.navArr------');
						//根据id来给dataArr进行赋值
						for(let i = 0; i < this.navArr.length;i++){
							
							if(this.navArr[i].id === this.Navindex){
								
								this.dataArr = [...this.navArr[i].list].reverse()
							}			
						}
					},
					fail: () => {},
					complete: () => {}
				});
				
				
			},
			//itme点击事件
			itmeClick(id){
				uni.navigateTo({
					url:`/pages/game-detail/game-detail?id=${id}`
				})
			},	
			//导航点击事件
			navClick(id){
				this.Navindex = id;
				if(id === 0){
					uni.switchTab({
						url: '/pages/index/index',
						success: res => {
							console.log(id,'switchTab-------------');
						},
						fail: () => {},
						complete: () => {}
					});
				}else{
					for(let i = 0; i < this.navArr.length;i++){
						
						if(this.navArr[i].id === this.Navindex){
							
							this.dataArr = [...this.navArr[i].list].reverse()
						}		
						
					}
				}				
				console.log(this.dataArr,'this.dataArr----------');	
			},
			
		},
		//监听Navindex改变时更新数组
		watch: {
			Navindex(newValue, oldValue) {
				console.log(newValue, oldValue,'watch-----------');
				for(let i = 0; i < this.navArr.length;i++){
					
					if(this.navArr[i].id === this.Navindex){
						
						this.dataArr = [...this.navArr[i].list].reverse()
					}							
				}
			}
		},

	}
</script>

<style lang="scss">
 /* nav */
 
		.nav-box {
			position: fixed;
			top: var(--window-top);
			white-space: nowrap;
			height: 30px;
			line-height: 30px;
			z-index: 9;
			padding-bottom: 10px;
			background-color: #000000;

			/* ::-webkit-scrollbar {
				width: 0px;
				height: 0px;
			} */

			.nav-list {
				display: flex;
				justify-content: space-between;
				color: #fff;

			}

			.nav-itme {
				display: inline-block;
				font-size: 36rpx;
				color: #fff;
				padding: 5rpx 35rpx;

				//margin: 5rpx 50rpx 0rpx 18rpx;
				&.active {
					color: #935559;
				}
			}
		}
		
		/* CBA */
		.cba-box{
			margin-top: 80rpx;
			background: linear-gradient(60deg, rgba(13,0,0,0.2) 3%, #fb5266 80%);
			.scroll-itme{
				/* position: relative; */
				display: inline-block;
				width: 700rpx;
				height: 392rpx;
				border-radius: 16px;
				margin: 10px 12px;
				/* background: linear-gradient(180deg, rgba(13,0,0,0.00) -3%, #fb5266 100%); */
				background: linear-gradient(180deg, #393939 -3%, #373B40 100%);
				box-sizing: border-box;
				border: 1px solid #05638e;
				
				.nba-logo{
					display: flex;
					justify-content: space-around;
					align-items: center;
					padding-top: 30%;
					transform: translate(0,-50%);
					
					.nba-img{
						width: 140rpx;
						height: 140rpx;
						
					}
					.number{
						color: #fff;
						font-size: 50rpx;
						
					}		
				}
				
				.nba-team{
					/* display: flex;
					justify-content: space-around;
					align-items: center;
					display: inline-block; */
					position: relative;
					color: #fff;
					font-size: 38rpx;
					.nba-team-name-left{
						position: absolute;
						left:55rpx ;
						bottom: 60px;
						
					}
					.nba-team-name-right{
						position: absolute;							
						right: 32rpx;
						bottom: 60px;
					}
				}
			
				.roundType{
					position: relative;
					color: #828282;
					font-size: 42rpx;
					text-align: center;
					bottom: 110rpx;
					&.active{
						color: #c92e0f;
					}
					
				}
				.startTime{
					position: relative;
					color: #fff;
					font-size: 38rpx;
					text-align: center;
					bottom: 30px;
				}
				
			}
		}
		
</style>
